package com.zhiwei.jenkins.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;

public class StringUtils {
    public static boolean isBlank(String str) {
        return !isNotBlank(str);
    }

    public static boolean isNotBlank(String str) {
        return str != null && !"".equals(str.trim());
    }

    public static String upperCaseFirstChar(String str) {
        return str.substring(0, 1).toUpperCase() + str.substring(1);
    }

    public static String lowerCaseFirstChar(String str) {
        return str.substring(0, 1).toLowerCase() + str.substring(1);
    }

    public static boolean equals(String str1, String str2) {
        return str1 == null ? str2 == null : str1.equals(str2);
    }

    /**
     * 字符串是否为数字
     */
    public static boolean isNumeric(String str) {
        for (int i = str.length(); --i >= 0;) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * 数组拼字符串
     *
     */
    public static String arr2string(String[] param, String split) {
        String content = "";
        if(null != param && param.length > 0){
            for (int i = 0; i < param.length; i++) {
                content = content + param[i] + split;
            }
            content = content.substring(0, content.length() - 1);
        }
        return content;
    }

    /**
     * ArrayList拼字符串
     *
     */
    public static String arr2string(ArrayList<String> param, String split) {
        String content = "";
        if(null != param && param.size() > 0){
            for (int i=0; i < param.size(); i++) {
                content = content + param.get(i) + split;
            }
            content = content.substring(0, content.length() - 1);
        }
        return content;
    }

    /**
     * Set拼字符串
     *
     */
    public static String arr2string(Set<String> param, String split) {
        String content = "";
        if(null != param && param.size() > 0){
            for (String item : param) {
                content = content + item + split;
            }
            content = content.substring(0, content.length() - 1);
        }
        return content;
    }

    //字符(串)==>Unicode编码(串)
    public static String str2unicode(String str)
    {
        char[] strArr = str.toCharArray();
        String unicode = "", temp;
        for(int index = 0;index < strArr.length; ++index)
        {
            temp = Integer.toHexString(strArr[index]).toUpperCase();
            while(temp.length()-4 != 0) temp = "0" + temp;
            unicode += "\\u" + temp;
        }
        return unicode;
    }

    //Unicode编码(串)==>字符(串)
    public static String unicode2str(String unicodeStr)
    {
        int beg = 0,end;
        String subStr;
        String result = "";
        while(beg != -1)
        {
            end = unicodeStr.indexOf("\\u", beg + 2);
            subStr = (-1 == end) ? unicodeStr.substring(beg + 2, unicodeStr.length()) :
                    unicodeStr.substring(beg + 2, end);
            result += String.valueOf((char)Integer.parseInt(subStr, 16));
            beg = end;
        }
        return result;
    }

    /**
     * 生成随机数字和字母组合
     * @param length
     * @return
     */
    public static String getCharAndNumber(int length) {
        String val = "";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            // 输出字母还是数字
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            // 字符串
            if ("char".equalsIgnoreCase(charOrNum)) {
                // 取得大写字母还是小写字母
                int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char) (choice + random.nextInt(26));
            } else if ("num".equalsIgnoreCase(charOrNum)) { // 数字
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }

    public static String stringFormat(String sourceStr, List<String> parmas) {
        if (null == parmas || 0 == parmas.size()) {
            return sourceStr;
        }

        for (int i = 1; i <= parmas.size(); i++) {
            String replaceStr = "{" + i + "}";
            sourceStr = sourceStr.replace(replaceStr, parmas.get(i-1));
        }

        return sourceStr;
    }

//    public static void main(String[] args ) {
//
//        // 短信内容参数
//        List<String> parmas = new ArrayList<>();
//        parmas.add("123456");
//        parmas.add("15分钟");
//        String str = StringUtils.stringFormat("您的验证码为{1}，请于{2}内正确输入，如非本人操作，请忽略此短信。", parmas);
//        System.out.println(str);
//    }
}
